#!/bin/bash

# sometimes the alsa device is LATE and 'aplay -l' delivers a empty result.
# in that case we would then create a new config.txt and trigger a reboot.
# after reboot we might be lucky, or do another loop.....
# Fix: be more patient: wait a couple of secs
for I in 1 2 3 4 5
do
  FOUND=`aplay -l|grep hifiberry | grep -v "pcm5102"`
  if [ ! -z "$FOUND" ]; then
    echo "alsa hifiberry device found. "  >> /var/log/hifiberry.log 2>&1
    break
  fi
  echo "no alsa hifiberry device present. waiting another 2 seconds..." >> /var/log/hifiberry.log 2>&1
  sleep 2
done

# On the Pi Zero, volume controls won't show up before playing something :(
SOFTVOL=`amixer | grep Softvol`
if [ "$SOFTVOL" == "" ]; then
 play -n trim 0 0.1
fi

FOUND=`aplay -l|grep hifiberry | grep -v "pcm5102"`
CONFIG=/boot/config.txt
REBOOTFILE=/tmp/reboot

function exit_and_cleanup() {
	sync
	mount -o ro,remount /boot
	exit
}

function check_i2c {
	modprobe i2c-dev
        if [ ! -e /dev/i2c-1 ]; then
                echo "I2C device file not found, need to modify config.txt"
                echo "dtparam=i2c1=on" >> $CONFIG
                echo "Enabling I2C" >> $REBOOTFILE
        fi
}

function check_hat {
	HATCARD=`/opt/hifiberry/bin/readhat | awk -F: '{print $2}'`
}

function detect_card {
	check_hat

	if [ "$HATCARD" == "Amp100" ]; then
                DETECTED="amp100,automute"
		EEPROM=0
                return
        fi

	if [ "$HATCARD" == "DAC+ ADC Pro" ]; then
		DETECTED="dacplusadcpro"
		return
	fi

	if [ "$HATCARD" == "DAC+ ADC" ]; then
                DETECTED="dacplusadc"
                return
        fi

	if [ "$HATCARD" == "DAC 2 HD" ]; then
		DETECTED="dacplushd"
		EEPROM=0
		return
	fi

	if [ "$HATCARD" == "Digi2 Pro" ]; then
        	DETECTED="digi-pro"
        	EEPROM=0
        	return
	fi
  
        if [ "$HATCARD" == "Amp3" ]; then
                DETECTED="amp3"
		EEPROM=0
                return
        fi

	if [ "$HATCARD" == "Amp4" ]; then
                DETECTED="amp4"
                EEPROM=0
                return
        fi

        # DAC+ ADC Pro
        res=`i2cget -y 1 0x4a 25 2>/dev/null`
        if [ "$res" == "0x07" ]; then
                DETECTEDT="dacplusadcpro"
		return
        fi

	# Digi/Digi+
	res=`i2cget -y 1 0x3b 1 2>/dev/null`
	if [ "$res" == "0x88" ]; then
		DETECTED="digi"
		return
	fi

	# DAC Plus
	res=`i2cget -y 1 0x4d 40 2>/dev/null`
	if [ "$res" == "0x02" ]; then
                DETECTED="dacplus"
		return
        fi

	# Amp/Amp+
	res=`i2cget -y 1 0x1b 0 2>/dev/null`
        if [ "$res" == "0x6c" ]; then
                DETECTED="amp"
		return
        fi
        if [ "$res" == "0x60" ]; then
                DETECTED="amp"
		return
        fi

	# DAC 2 HD
	res=`i2cget -yf 1 0x62 17 2>/dev/null`
	if [ "$res" == "0x8c" ]; then
		DETECTED="dacplushd"
		EEPROM=0
		return
	fi

        # Beocreate
        res=`i2cget -y 1 0x60 2 2>/dev/null`
        if [ "$res" == "0x03" ]; then
                DETECTED="beo"
		return
        fi

        # Some I2C addresses might be blocked from HAT EEPROM, but hifibarry-dac is used
        res=`i2cdetect -y 1 | grep UU`
        if [ "$res" != "" ]; then
                DETECTED="blocked"
        fi
}

if [ "$FOUND" == "" ]; then
	echo "No HiFiBerry card configured, try to probe using I2C" >> /var/log/hifiberry.log

	echo "Mounting /boot"
	mount -o remount,rw /boot
	if [ ! -f /boot/config.txt ]; then
		echo "couldn't mount FAT file system, aborting card detection" >> /var/log/hifiberry.log
   		exit 1
 	fi

	echo "Detecting using I2C" 
	check_i2c
        detect_card
	card=$DETECTED

	if [ "$card" == "beo" ]; then
		echo "Detected Beocreate"
		FOUND2=`aplay -l|grep hifiberry`
		if [ "$FOUND2" != "" ]; then
			exit_and_cleanup
		fi
		card=dac
	fi

	if [ "card" == "blocked" ]; then
		FOUND2=`aplay -l|grep hifiberry-dac`
		if [ "$FOUND2" != "" ]; then
			echo "HiFiBerry DAC driver used, but some card might be configured using HAT EEPROM"
			cat /boot/config.txt | grep -v "hifiberry-dac" > /boot/config.txt2
			diff /boot/config.txt /boot/config.txt2
			if [ "$?" != 0 ]; then
				echo "Found HiFiBerry DAC configuration, removing it" >>/var/log/hifiberry.log
				mv /boot/config.txt2 /boot/config.txt
				echo "Removing hifiberry-dac overlay" >> $REBOOTFILE
			fi

		fi
	fi

	if [ "$card" != "" ]; then
		echo "Detected $card" >>/var/log/hifiberry.log
		echo "Creating new config.txt" >>/var/log/hifiberry.log
		cat $CONFIG | grep -v "hifiberry" > /tmp/config.txt 
		echo "dtoverlay=hifiberry-$card" >> /tmp/config.txt
		if [ "$EEPROM" == "0" ]; then
			HAS_EEPROM=`cat /tmp/config.txt | grep "force_eeprom_read=0"`
			if [ "$HAS_EEPROM" == "" ]; then
				echo "force_eeprom_read=0" >> /tmp/config.txt
			fi
		fi
		mv /tmp/config.txt $CONFIG
		echo "Adding $card to config.txt" >> $REBOOTFILE
	else
		echo "Could not find an I2C enabled sound card"
		cat /boot/config.txt | grep -v "hifiberry-digi" | grep -v "hifiberry-amp" | grep -v "hifiberry-dacplus" > /boot/config.txt2
		diff /boot/config.txt /boot/config.txt2
		if [ "$?" != 0 ]; then
			echo "Found old configuration, removing it" >>/var/log/hifiberry.log
			mv /boot/config.txt2 /boot/config.txt
			echo "Removing old configuration in config.txt" >> $REBOOTFILE
		fi
		echo "Assuming HiFiBerry DAC+ Light, Zero"
		FOUND2=`aplay -l|grep hifiberry`
		if [ "$FOUND2" != "" ]; then
			exit_and_cleanup
		else
			echo "dtoverlay=hifiberry-dac" >> /boot/config.txt
			echo "Nothing detected, assuming HiFiBerry DAC+ Light/Zero" >>/var/log/hifiberry.log
			echo "Enabling DAC+ Light" >> $REBOOTFILE
		fi
	fi
else
   echo "Found HiFiBerry card: $FOUND" >>/var/log/hifiberry.log
fi

exit_and_cleanup
